package com.example.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.req.IdParam;
import com.example.common.resp.Result;
import com.example.springboot.dto.RoutingResp;
import com.example.springboot.entity.RoleEntity;
import com.example.springboot.entity.RoutingEntity;
import com.example.springboot.entity.SubmenuEntity;
import com.example.springboot.service.RoleService;
import com.example.springboot.service.RoutingService;
import com.example.springboot.service.SubmenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 权限路由表 前端控制器
 * </p>
 *
 * @author he
 * @since 2023-11-16
 */
@Tag(name = "权限路由关联")
@RestController
@RequestMapping("/routingEntity")
public class RoutingController {

    @Autowired
    private RoutingService routingService;
    @Autowired
    private SubmenuService submenuService;
    @Autowired
    private RoleService roleService;

    /**
     * @param param
     * @return
     */
    @Operation(summary = "是否存在")
    @PostMapping("/exist")
    public Integer exist(@RequestBody @Validated IdParam param) {
        QueryWrapper<RoutingEntity> routingEntityQueryWrapper = new QueryWrapper<>();
        routingEntityQueryWrapper.eq("id", param.getId());
        if (routingService.count(routingEntityQueryWrapper) == 0) {
            return 0;
        }
        return 1;
    }

    @Operation(summary = "关联列表")
    @GetMapping("/list")
    public Result<List<RoutingResp>> list() {
        List<RoutingEntity> routingEntityList = routingService.list();
        ArrayList<RoutingResp> routingRespArrayList = new ArrayList<>();
        for (int i = 0; i < routingEntityList.size(); i++) {
            RoutingEntity routingEntity = routingEntityList.get(i);
            RoutingResp routingResp = new RoutingResp();
            BeanUtils.copyProperties(routingEntity, routingResp);
            //向下搜索
            routingResp.setRoleEntity(roleService.getById(routingEntity.getRoleId()));
            routingResp.setSubmenuEntity(submenuService.getById(routingEntity.getSubmenuId()));
            //添加
            if (routingEntity.getSubmenuId() != 0) {
                routingRespArrayList.add(routingResp);
            }
        }
        return Result.success(routingRespArrayList);
    }

    @Operation(summary = "根据id查找")
    @PostMapping("/detail")
    public Result<RoutingResp> detail(@RequestBody @Validated IdParam param) {
        //id是否存在
        if (exist(param) == 0) {
            return Result.failure("关联不存在");
        }
        //查找关联
        RoutingResp routingResp = new RoutingResp();
        RoutingEntity routingEntity = routingService.getById(param.getId());
        BeanUtils.copyProperties(routingEntity, routingResp);
        //向下查找
        routingResp.setSubmenuEntity(submenuService.getById(routingEntity.getSubmenuId()));
        routingResp.setRoleEntity(roleService.getById(routingEntity.getRoleId()));
        return Result.success(routingResp);
    }

    @Operation(summary = "保存或修改")
    @PostMapping("/save")
    public Result save(@RequestBody @Validated RoutingEntity param) {
        RoutingEntity routingEntity = new RoutingEntity();
        BeanUtils.copyProperties(param, routingEntity);
        //父路由ID是否存在
        QueryWrapper<SubmenuEntity> submenuEntityQueryWrapper = new QueryWrapper<>();
        submenuEntityQueryWrapper.eq("id", routingEntity.getSubmenuId());
        if (submenuService.count(submenuEntityQueryWrapper) == 0) {
            return Result.failure("父路由ID不存在");
        }
        //权限ID是否存在
        QueryWrapper<RoleEntity> roleEntityQueryWrapper = new QueryWrapper<>();
        roleEntityQueryWrapper.eq("id", routingEntity.getRoleId());
        if (roleService.count(roleEntityQueryWrapper) == 0) {
            return Result.failure("权限ID不存在");
        }
        //是否重复
        List<RoutingEntity> routingEntityList = routingService.list();
        for (int i = 0; i < routingEntityList.size(); i++) {
            RoutingEntity routing = routingEntityList.get(i);
            if (Objects.equals(routing.getSubmenuId(), param.getSubmenuId()) && Objects.equals(routing.getRoleId(), param.getRoleId())) {
                return Result.failure("关联已存在");
            }
        }
        //保存||修改
        if (routingEntity.getId() == 0) {
            routingEntity.setId(null);
            if (routingService.save(routingEntity)) {
                return Result.success();
            }
            return Result.failure("保存失败，请稍后重试");
        } else {
            if (routingService.updateById(routingEntity)) {
                return Result.success();
            }
            return Result.failure("修改失败，请稍后重试");
        }
    }

    @Operation(summary = "根据id删除")
    @PostMapping("/delete")
    public Result delete(@RequestBody @Validated IdParam param) {
        //ID是否存在
        if (exist(param) == 0) {
            return Result.failure("关联不存在");
        }
        //删除
        if (routingService.removeById(param.getId())) {
            return Result.success();
        } else {
            return Result.failure("删除失败，请稍后重试");
        }
    }
}
